package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {

	public Standard standard = new Standard();

	@Override
	public Standard getModel() {
		return standard;
	}

	// Service 注入
	@Autowired
	private StandardService servies;

	@Action(value = "standard_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
	public String save() {
		servies.save(standard);
		System.out.println("添加数据..............");
		return SUCCESS;
	}
	/*
	 * // 属性驱动 private int page; private int rows;
	 * 
	 * public void setPage(int page) { this.page = page; }
	 * 
	 * public void setRows(int rows) { this.rows = rows; }
	 * 
	 * // 分页查询 列表
	 * 
	 * @Action(value = "standard_pageQuery", results = { @Result(name =
	 * "success", type = "json") }) public String pageQuery() { // 调用业务层查询数据结果
	 * // 获取目标查询数据 org.springframework.data.domain.Pageable pageable = new
	 * PageRequest(page - 1, rows); Page<Standard> pageData =
	 * servies.findPageService(pageable); // 返回客户端 需要 total 和 rows Map<String,
	 * Object> result = new HashMap<String, Object>(); result.put("total",
	 * pageData.getNumberOfElements()); result.put("rows",
	 * pageData.getContent()); // 江苏局转换成Json
	 * ActionContext.getContext().getValueStack().push(result); return SUCCESS;
	 * 
	 * }
	 */

	// 分页查询显示

	// 属性驱动
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	// 分页查询
	@Action(value = "standard_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageData() {
		// 接受前台数据 将 前天数据进行封装 方便调用
		Pageable pageable = new PageRequest(page - 1, rows);
		// 调用业务层
		Page<Standard> pageData = servies.findDataService(pageable);

		// 接受Service数据
		Map<String, Object> request = new HashMap<String, Object>();
		request.put("total", pageData.getNumberOfElements());
		request.put("rows", pageData.getContent());

		// 将Map 进行转换 Json
		ActionContext.getContext().getValueStack().push(request);

		return SUCCESS;
	}

	// 查询收派标准
	@Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
	public String findAll() {
		List<Standard> standard = servies.findAll();
		// 转换成Json
		ActionContext.getContext().getValueStack().push(standard);
		return SUCCESS;
	}

}
